<!--  -->
<template>
  <div class="todo-footer">
    <label>
      <input type="checkbox"
             v-model="isAll" />
      <!-- @click="selectAll" -->
    </label>
    <span>
      <span>已完成{{finishItem}}</span> / 全部{{total}}
    </span>
    <button class="
             btn
             btn-danger"
            @click="clearItem">清除已完成任务</button>
  </div>
</template>

<script>
export default {
  name: 'Footer',
  data () {
    return {

    }
  },
  props: ["todos", 'updateAll', "clearAll"],
  computed: {
    total () {
      return this.todos.length;
    },
    finishItem () {
      //第一种方法：foreach
      /*  let count = 0;
       this.todos.forEach((item, index) => {
         if (item.done == true) {
           count++;
         }
       }) 
        return count
       */
      // 第二种：reduce方法
      return this.todos.reduce((pre, current) => pre += current.done ? 1 : 0, 0)
    },
    isAll: {
      // 使用计算属性的set去反选
      set (flag) {
        // console.log(flag);
        this.updateAll(flag)
      },
      get () {
        let bool = this.total === this.finishItem && this.total > 0
        return bool
      }
    }
  },
  methods: {
    //1.使用方法去实现全反选
    selectAll (event) {
      // console.log(event.target.checked);
      this.updateAll(event.target.checked)
    },
    //2.清除已完成任务
    clearItem () {
      if (confirm("你确定要清除已完成任务吗？")) {
        
        this.clearAll()
      }
    }
  }
}
</script>



<style scoped>
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}
.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}
.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}
.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>